package com.jing.flashlight;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.jing.flashlight.handler.GetWebInfoHandler;
import com.jing.flashlight.model.WebInfo;
import com.jing.flashlight.net.RequestListener;

public class HelperActivity extends Activity {

	private WebView web;
	private Handler handler;
	private View conent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_help);
		handler = new Handler();
		init();
		fetchData();
	}

	private void init() {
		View c = findViewById(R.id.close);
		c.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		conent = findViewById(R.id.tip_content);
		TextView t = (TextView) findViewById(R.id.t_c);
		t.setText(getString(R.string.about_content, getClientVersionName(this)));
		web = (WebView) findViewById(R.id.web);
		final WebSettings webSettings = web.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setUseWideViewPort(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setBuiltInZoomControls(false);
		webSettings.setAllowFileAccess(true);
		webSettings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);
		webSettings.setSupportMultipleWindows(true);
		web.setWebChromeClient(new WebChromeClient());
		web.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
			}

			@Override
			public void onPageFinished(WebView view, String url) {
			}

			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				handler.proceed();
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
	}

	private void fetchData() {
		final GetWebInfoHandler gih = new GetWebInfoHandler(this);
		gih.onRequest(new RequestListener() {

			@Override
			public void onError(Object error) {
				
			}

			@Override
			public void onCallBack(Object data) {
				final WebInfo info = (WebInfo) gih.getData();
				handler.post(new Runnable() {

					@Override
					public void run() {
						String url = info.getUrl();
						if (TextUtils.isEmpty(url)) {
							web.setVisibility(View.GONE);
							conent.setVisibility(View.VISIBLE);
						} else {
							Toast.makeText(HelperActivity.this, url, Toast.LENGTH_LONG).show();
							web.loadUrl(url);
							web.setVisibility(View.VISIBLE);
							conent.setVisibility(View.GONE);
						}
					}
				});
			}
		}, false);
	}

	public String getClientVersionName(Context context) {
		PackageInfo packageInfo;
		try {
			packageInfo = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
			String versionName = "";
			if (packageInfo != null) {
				versionName = packageInfo.versionName;
			}
			return versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (web != null && web.getVisibility() == View.VISIBLE) {
				if (web.canGoBack()) {
					web.goBack();
					return true;
				}else {
					finish();
				}
			}
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
